package cn.chinaflame.pagodas.schedul;

import cn.chinaflame.pagodas.constant.Sysconst;
import cn.chinaflame.pagodas.entity.PaOrder;
import cn.chinaflame.pagodas.entity.PaOrderitem;
import cn.chinaflame.pagodas.entity.PaProductPoint;
import cn.chinaflame.pagodas.enums.CodeEnum;
import cn.chinaflame.pagodas.handle.HandlerException;
import cn.chinaflame.pagodas.repository.PaOrderRepository;
import cn.chinaflame.pagodas.repository.PaOrderitemRepository;
import cn.chinaflame.pagodas.repository.PaProductPointRepository;
import cn.chinaflame.pagodas.service.PaOrderService;
import cn.chinaflame.pagodas.service.PaProductPointService;
import cn.chinaflame.pagodas.service.WrmNIService;
import cn.chinaflame.pagodas.spring.ApplicationContextHolder;
import cn.chinaflame.pagodas.vo.LightVO;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author wwt
 * @date 2018年1月17日 上午10:00:25
 */
public class PaOrderFailure implements Runnable {


    private static final Logger log = LoggerFactory.getLogger(PaOrderFailure.class);

    private PaOrder paOrder;
    private static Lock lock = new ReentrantLock();


    public PaOrderFailure(PaOrder paOrder) {
        this.paOrder = paOrder;
    }

    @Override
    public void run() {
        log.info("订单定时任务进入");
        paOrder = ApplicationContextHolder.getBean(PaOrderRepository.class).findOne(paOrder.getId());
        if (Sysconst.PAY_STATUS_SUC.equals(paOrder.getPayStatus())) {
            lock.lock();
            this.cgCallBack(paOrder);
        } else if (Sysconst.PAY_STATUS_PART.equals(paOrder.getPayStatus())) {
            lock.lock();
            log.info("订单部分支付，不执行操作，等待支付完成");
        }else if (!Sysconst.PAY_STATUS_SUC.equals(paOrder.getPayStatus())){
            lock.lock();
            try {
                this.callBack(paOrder);
            }catch (Exception e) {
                log.error("异常信息", e);
                throw new HandlerException(CodeEnum.FAIL.getCode(), "系统异常", "系统异常", e);
            }finally {
                lock.unlock();
            }
        }

    }
    //支付付款任务
    public static void  cgCallBack (PaOrder paOrder) {
        log.info("支付成功回调后操作 id:{}",paOrder.getId());
        try{
            //点灯操作
            if (paOrder.getOrderType().equals("1") || paOrder.getOrderType().equals("2")) {
                List<PaOrderitem> list =  ApplicationContextHolder.getBean(PaOrderitemRepository.class).findByOrderId(paOrder.getId());
                PaOrderitem paOrderitem = list.get(0);
                JSONArray param = new JSONArray();
                JSONObject p = new JSONObject();
                p.put("lightno", paOrderitem.getLocationNo());
                p.put("obj", paOrderitem.getBeWishedName());
                p.put("bless", paOrderitem.getBeWishedMemo());
                param.add(p);
                LightVO lightVO = ApplicationContextHolder.getBean(WrmNIService.class).wwtLightSend(
                        paOrderitem.getLocationNo(),paOrderitem.getBeWishedName(),paOrderitem.getBeWishedMemo());
                //接口返回状态为成功
                if (1 == lightVO.getResult()) {
                    PaProductPoint paProductPoint = ApplicationContextHolder.getBean(PaProductPointRepository.class).
                            findByOrderitemIdAndLocationNoAndStatus(paOrderitem.getId(),paOrderitem.getLocationNo(),"1");
                    paProductPoint.setStatus("4");
                    ApplicationContextHolder.getBean(PaProductPointRepository.class).saveAndFlush(paProductPoint);
                    paOrderitem.setApiStatus("2");
                    ApplicationContextHolder.getBean(PaOrderitemRepository.class).saveAndFlush(paOrderitem);
                    //接口返回状态为失败
                }else {
                    paOrderitem.setApiStatus("3");
                    ApplicationContextHolder.getBean(PaOrderitemRepository.class).saveAndFlush(paOrderitem);
                }
            //更改祝福语操作
            }else if (paOrder.getOrderType().equals("5")) {
                List<PaOrderitem> list =  ApplicationContextHolder.getBean(PaOrderitemRepository.class).findByOrderId(paOrder.getId());
                PaOrderitem paOrderitem = list.get(0);
                JSONArray param = new JSONArray();
                JSONObject p = new JSONObject();
                p.put("lightno", paOrderitem.getLocationNo());
                p.put("obj", paOrderitem.getBeWishedName());
                p.put("bless", paOrderitem.getBeWishedMemo());
                param.add(p);
                LightVO lightVO = ApplicationContextHolder.getBean(WrmNIService.class).wwtLightSend(
                        paOrderitem.getLocationNo(),paOrderitem.getBeWishedName(),paOrderitem.getBeWishedMemo());
                //接口返回状态为成功
                if (1 == lightVO.getResult()) {
                    paOrderitem.setApiStatus("2");
                    ApplicationContextHolder.getBean(PaOrderitemRepository.class).saveAndFlush(paOrderitem);
                //接口返回状态为成功
                }else {
                    paOrderitem.setApiStatus("3");
                    ApplicationContextHolder.getBean(PaOrderitemRepository.class).saveAndFlush(paOrderitem);
                }
            //闪灯操作
            }else if (paOrder.getOrderType().equals("6")) {
                List<PaOrderitem> list =  ApplicationContextHolder.getBean(PaOrderitemRepository.class).findByOrderId(paOrder.getId());
                PaOrderitem paOrderitem = list.get(0);
                LightVO LightVO = ApplicationContextHolder.getBean(WrmNIService.class).wwtLightSearch(paOrderitem.getLocationNo());
                if ("1".equals(LightVO.getResult())) {
                    paOrderitem.setApiStatus("2");
                    ApplicationContextHolder.getBean(PaOrderitemRepository.class).saveAndFlush(paOrderitem);
                    //接口返回状态为成功
                }else {
                    paOrderitem.setApiStatus("3");
                    ApplicationContextHolder.getBean(PaOrderitemRepository.class).saveAndFlush(paOrderitem);
                }
            }
        }catch (Exception e){
            log.error("异常信息", e);
            throw new HandlerException(CodeEnum.FAIL.getCode(), "系统异常", "系统异常", e);
        }finally {
            lock.unlock();
        }
    }

    //执行未付款任务
    public static void callBack(PaOrder paOrder) {
        log.info("未支付撤销订单操作 id:{}",paOrder.getId());
        try{
            if (Sysconst.PAY_STATUS_WAIT.equals(paOrder.getPayStatus())) {
                paOrder.setPayStatus(Sysconst.PAY_STATUS_PAY_TIMEOUT);
            }else if (Sysconst.PAY_STATUS_DOING.equals(paOrder.getPayStatus())){
                paOrder.setPayStatus(Sysconst.PAY_STATUS_FAIL);
            }
            List<PaOrderitem> list =  ApplicationContextHolder.getBean(PaOrderitemRepository.class).findByOrderId(paOrder.getId());
            for (PaOrderitem paOrderitem : list){
                PaProductPoint paProductPoint = ApplicationContextHolder.getBean(PaProductPointRepository.class).findByOrderitemIdAndLocationNoAndStatus(
                        paOrderitem.getId(),paOrderitem.getLocationNo(),Sysconst.POINT_STATUS_LOCK);
                paProductPoint.setOrderitemId("");
                paProductPoint.setStatus("1");
                ApplicationContextHolder.getBean(PaProductPointRepository.class).save(paProductPoint);
            }
            ApplicationContextHolder.getBean(PaOrderRepository.class).save(paOrder);
        }catch (Exception e){
            log.error("异常信息", e);
            throw new HandlerException(CodeEnum.FAIL.getCode(), "系统异常", "系统异常", e);
        }finally {
            lock.unlock();
        }
    }

}
